﻿using System;

namespace HslCommunication.Core.Address
{
	/// <summary>
	/// 西门子的地址数据信息，主要包含数据代码，DB块，偏移地址，当处于写入时，Length无效<br />
	/// Address data information of Siemens, mainly including data code, DB block, offset address, when writing, Length is invalid
	/// </summary>
	// Token: 0x0200016B RID: 363
	public class S7AddressData : DeviceAddressDataBase
	{
		/// <summary>
		/// 获取或设置等待读取的数据的代码<br />
		/// Get or set the code of the data waiting to be read
		/// </summary>
		// Token: 0x1700061C RID: 1564
		// (get) Token: 0x06001DFB RID: 7675 RVA: 0x0008FBD0 File Offset: 0x0008DDD0
		// (set) Token: 0x06001DFC RID: 7676 RVA: 0x0008FBD8 File Offset: 0x0008DDD8
		public byte DataCode { get; set; }

		/// <summary>
		/// 获取或设置PLC的DB块数据信息<br />
		/// Get or set PLC DB data information
		/// </summary>
		// Token: 0x1700061D RID: 1565
		// (get) Token: 0x06001DFD RID: 7677 RVA: 0x0008FBE1 File Offset: 0x0008DDE1
		// (set) Token: 0x06001DFE RID: 7678 RVA: 0x0008FBE9 File Offset: 0x0008DDE9
		public ushort DbBlock { get; set; }

		/// <summary>
		/// 从指定的地址信息解析成真正的设备地址信息
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <param name="length">数据长度</param>
		// Token: 0x06001DFF RID: 7679 RVA: 0x0008FBF4 File Offset: 0x0008DDF4
		public override void Parse(string address, ushort length)
		{
			OperateResult<S7AddressData> operateResult = S7AddressData.ParseFrom(address, length);
			bool isSuccess = operateResult.IsSuccess;
			if (isSuccess)
			{
				base.AddressStart = operateResult.Content.AddressStart;
				base.Length = operateResult.Content.Length;
				this.DataCode = operateResult.Content.DataCode;
				this.DbBlock = operateResult.Content.DbBlock;
			}
		}

		/// <summary>
		/// 计算特殊的地址信息<br />
		/// Calculate Special Address information
		/// </summary>
		/// <param name="address">字符串地址 -&gt; String address</param>
		/// <param name="isCT">是否是定时器和计数器的地址</param>
		/// <returns>实际值 -&gt; Actual value</returns>
		// Token: 0x06001E00 RID: 7680 RVA: 0x0008FC60 File Offset: 0x0008DE60
		public static int CalculateAddressStarted(string address, bool isCT = false)
		{
			bool flag = address.IndexOf('.') < 0;
			int result;
			if (flag)
			{
				if (isCT)
				{
					result = Convert.ToInt32(address);
				}
				else
				{
					result = Convert.ToInt32(address) * 8;
				}
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					'.'
				});
				result = Convert.ToInt32(array[0]) * 8 + Convert.ToInt32(array[1]);
			}
			return result;
		}

		/// <summary>
		/// 从实际的西门子的地址里面解析出地址对象<br />
		/// Resolve the address object from the actual Siemens address
		/// </summary>
		/// <param name="address">西门子的地址数据信息</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001E01 RID: 7681 RVA: 0x0008FCC4 File Offset: 0x0008DEC4
		public static OperateResult<S7AddressData> ParseFrom(string address)
		{
			return S7AddressData.ParseFrom(address, 0);
		}

		/// <summary>
		/// 从实际的西门子的地址里面解析出地址对象<br />
		/// Resolve the address object from the actual Siemens address
		/// </summary>
		/// <param name="address">西门子的地址数据信息</param>
		/// <param name="length">读取的数据长度</param>
		/// <returns>是否成功的结果对象</returns>
		// Token: 0x06001E02 RID: 7682 RVA: 0x0008FCE0 File Offset: 0x0008DEE0
		public static OperateResult<S7AddressData> ParseFrom(string address, ushort length)
		{
			S7AddressData s7AddressData = new S7AddressData();
			try
			{
				s7AddressData.Length = length;
				s7AddressData.DbBlock = 0;
				bool flag = address[0] == 'I';
				if (flag)
				{
					s7AddressData.DataCode = 129;
					s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
				}
				else
				{
					bool flag2 = address[0] == 'Q';
					if (flag2)
					{
						s7AddressData.DataCode = 130;
						s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
					}
					else
					{
						bool flag3 = address[0] == 'M';
						if (flag3)
						{
							s7AddressData.DataCode = 131;
							s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
						}
						else
						{
							bool flag4 = address[0] == 'D' || address.Substring(0, 2) == "DB";
							if (flag4)
							{
								s7AddressData.DataCode = 132;
								string[] array = address.Split(new char[]
								{
									'.'
								});
								bool flag5 = address[1] == 'B';
								if (flag5)
								{
									s7AddressData.DbBlock = Convert.ToUInt16(array[0].Substring(2));
								}
								else
								{
									s7AddressData.DbBlock = Convert.ToUInt16(array[0].Substring(1));
								}
								bool flag6 = array[1].StartsWith("DBX") || array[1].StartsWith("DBB") || array[1].StartsWith("DBW") || array[1].StartsWith("DBD");
								if (flag6)
								{
									array[1] = array[1].Substring(3);
								}
								s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(array[1], false);
							}
							else
							{
								bool flag7 = address[0] == 'T';
								if (flag7)
								{
									s7AddressData.DataCode = 31;
									s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), true);
								}
								else
								{
									bool flag8 = address[0] == 'C';
									if (flag8)
									{
										s7AddressData.DataCode = 30;
										s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), true);
									}
									else
									{
										bool flag9 = address[0] == 'V';
										if (!flag9)
										{
											return new OperateResult<S7AddressData>(StringResources.Language.NotSupportedDataType);
										}
										s7AddressData.DataCode = 132;
										s7AddressData.DbBlock = 1;
										s7AddressData.AddressStart = S7AddressData.CalculateAddressStarted(address.Substring(1), false);
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				return new OperateResult<S7AddressData>(ex.Message);
			}
			return OperateResult.CreateSuccessResult<S7AddressData>(s7AddressData);
		}
	}
}
